home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Resources / Online / Term / Extras / Source / gtlayout-source.lha / LT_LevelWidth.c < prev    next >
C/C++ Source or Header  |  1996-08-22  |  10KB  |  331 lines

  1. /*
  2. **    GadTools layout toolkit
  3. **
  4. **    Copyright © 1993-1996 by Olaf `Olsen' Barthel
  5. **        Freely distributable.
  6. **
  7. **    :ts=4
  8. */
  9.  
  10. #ifndef _GTLAYOUT_GLOBAL_H
  11. #include "gtlayout_global.h"
  12. #endif
  13.  
  14. STATIC LONG __stdargs
  15. DefaultDispFunc(struct Gadget *gad,LONG value)
  16. {
  17.     return(value);
  18. }
  19.  
  20.  
  21. /*****************************************************************************/
  22.  
  23.  
  24. VOID
  25. LTP_LevelWidth(LayoutHandle *handle,STRPTR levelFormat,DISPFUNC dispFunc,LONG min,LONG max,LONG *maxWidth,LONG *maxLen,BOOL fullCheck)
  26. {
  27.     UBYTE buffer[80];
  28.     LONG localMaxWidth;
  29.     LONG localMaxLen;
  30.     LONG len;
  31.  
  32.     localMaxWidth    = 0;
  33.     localMaxLen    = 0;
  34.  
  35.     if(handle)
  36.     {
  37.         if(!dispFunc)
  38.             dispFunc = (DISPFUNC)DefaultDispFunc;
  39.  
  40.         if(!levelFormat)
  41.         {
  42.             if(LocaleBase && LocaleBase->lb_SysPatches)
  43.                 levelFormat = "%lD";
  44.             else
  45.                 levelFormat = "%ld";
  46.         }
  47.  
  48.         if(fullCheck)
  49.         {
  50.             LONG i;
  51.  
  52.             for(i = min ; i <= max ; i++)
  53.             {
  54.                 SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,i));
  55.  
  56.                 len = strlen(buffer);
  57.  
  58.                 if(len > localMaxLen)
  59.                     localMaxLen = len;
  60.  
  61.                 len = TextLength(&handle->RPort,buffer,len);
  62.  
  63.                 if(len > localMaxWidth)
  64.                     localMaxWidth = len;
  65.             }
  66.         }
  67.         else
  68.         {
  69.             SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,min));
  70.  
  71.             len = strlen(buffer);
  72.  
  73.             if(len > localMaxLen)
  74.                 localMaxLen = len;
  75.  
  76.             len = TextLength(&handle->RPort,buffer,len);
  77.  
  78.             if(len > localMaxWidth)
  79.                 localMaxWidth = len;
  80.  
  81.             SPrintf(buffer,levelFormat,(LONG)(*dispFunc)(NULL,max));
  82.  
  83.             len = strlen(buffer);
  84.  
  85.             if(len > localMaxLen)
  86.                 localMaxLen = len;
  87.  
  88.             len = TextLength(&handle->RPort,buffer,len);
  89.  
  90.             if(len > localMaxWidth)
  91.                 localMaxWidth = len;
  92.         }
  93.     }
  94.  
  95.     if(maxLen)
  96.         *maxLen = localMaxLen;
  97.  
  98.     if(maxWidth)
  99.         *maxWidth = localMaxWidth + handle->GlyphWidth;
  100. }
  101.  
  102.  
  103. /*****************************************************************************/
  104.  
  105.  
  106. /****** gtlayout.library/LT_LevelWidth ******************************************
  107. *
  108. *   NAME
  109. *    LT_LevelWidth -- Determine the maximum width of a SLIDER_KIND
  110. *                     level string.
  111. *
  112. *   SYNOPSIS
  113. *    Index = LT_LevelWidth(Handle,FormatString,DispFunc,Min,Max,MaxWidth,
  114. *      D0                    A0        A1         A2     D0  D1    A3
  115. *
  116. *                              MaxLen,FullCheck);
  117. *                                A5      D2
  118. *
  119. *    LONG LT_LevelWidth(LayoutHandle *,STRPTR,
  120. *                       LONG (*)(struct Gadget *,WORD),LONG,LONG,LONG *,
  121. *                       LONG *,BOOL);
  122. *
  123. *   FUNCTION
  124. *    In order to make room for the level text displayed by a
  125. *    SLIDER_KIND object one needs to know how much space the
  126. *    longest level string will occupy. Otherwise, the level
  127. *    text may overwrite the gadget label text or the slider
  128. *    container. This routine will rattle through all possible
  129. *    slider settings (as given via the Min and the Max
  130. *    level values) and determine the longest label string
  131. *    according to the font used.
  132. *
  133. *   INPUTS
  134. *    Handle - Pointer to a LayoutHandle structure, as returned by
  135. *        a call to LT_CreateHandleTags().
  136. *
  137. *    FormatString - The sprintf() style formatting string to be used
  138. *        to format the slider level settings into text.
  139. *        This is the same string you would pass in via
  140. *        the GTSL_LevelFormat tag when creating the
  141. *        slider object.
  142. *        Default: "%lD" for systems which have locale.library
  143. *            installed, "%ld" otherwise.
  144. *
  145. *    DispFunc - A pointer to the function to filter the slider level
  146. *        values. The result of this function will then be
  147. *        used to format a string into the slider level text.
  148. *        This is the same parameter you would pass in via
  149. *        the GTSL_DispFunc tag when creating the slider
  150. *        object.
  151. *
  152. *            NOTE: the routine will be called with a NULL Gadget
  153. *                parameter, make sure your code will handle
  154. *                this nicely.
  155. *
  156. *        Default: no display function
  157. *
  158. *    Min - The smallest value the slider can be set to. This is
  159. *        same value you would pass in via GTSL_Min when creating
  160. *        the slider object.
  161. *
  162. *    Max - The largest value the slider can be set to. This is
  163. *        same value you would pass in via GTSL_Max when creating
  164. *        the slider object.
  165. *
  166. *    MaxWidth - Pointer to a place to store the width of the
  167. *        longest level string in pixels. If you pass
  168. *        in NULL instead of the address of a variable
  169. *        no harm will be done.
  170. *
  171. *    MaxLen - Pointer to a place to store the length of the
  172. *        longest level string in characters. If you pass
  173. *        in NULL instead of the address of a variable
  174. *        no harm will be done.
  175. *
  176. *    FullCheck - TRUE will cause the code to rattle through all
  177. *        possible slider settings, starting from the
  178. *        minimum value, ending at the maximum value.
  179. *        While this may be a good idea for a display
  180. *        function to map slider levels to text strings
  181. *        of varying length it might be a problem when
  182. *        it comes to display a range of numbers from
  183. *        1 to 40,000: the code will loop through
  184. *        40,000 iterations trying to find the longest
  185. *        string.
  186. *
  187. *        FALSE will cause the code to calculate the
  188. *        longest level string based only on the
  189. *        minimum and the maximum value to check.
  190. *        While this is certainly a good a idea when
  191. *        it comes to display a range of numbers from
  192. *        1 to 40,000 as only two values will be
  193. *        checked the code may fail to produce
  194. *        accurate results for sliders using display
  195. *        functions mapping slider levels to strings.
  196. *
  197. *   RESULT
  198. *    Index - The slider level which gives the longest
  199. *        level string.
  200. *
  201. *   NOTES
  202. *    Some compilers have trouble passing parameters in A5. In such
  203. *    a case it is recommended to use gtlayout.library/LT_NewLevelWidth
  204. *    instead.
  205. *
  206. *   SEE ALSO
  207. *    gtlayout.library/LT_NewLevelWidth
  208. *
  209. ******************************************************************************
  210. *
  211. */
  212.  
  213. VOID LIBENT
  214. LT_LevelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR levelFormat,REG(a2) DISPFUNC dispFunc,REG(d0) LONG min,REG(d1) LONG max,REG(a3) LONG *maxWidth,REG(a5) LONG *maxLen,REG(d2) BOOL fullCheck)
  215. {
  216.     LTP_LevelWidth(handle,levelFormat,dispFunc,min,max,maxWidth,maxLen,fullCheck);
  217. }
  218.  
  219. /****** gtlayout.library/LT_NewLevelWidth ******************************************
  220. *
  221. *   NAME
  222. *    LT_NewLevelWidth -- Determine the maximum width of a SLIDER_KIND
  223. *                        level string. (V14)
  224. *
  225. *   SYNOPSIS
  226. *    Index = LT_LevelWidth(Handle,FormatString,DispFunc,Min,Max,MaxWidth,
  227. *      D0                    A0        A1         A2     D0  D1    A3
  228. *
  229. *                              MaxLen,FullCheck);
  230. *                                D3      D2
  231. *
  232. *    LONG LT_LevelWidth(LayoutHandle *,STRPTR,
  233. *                       LONG (*)(struct Gadget *,WORD),LONG,LONG,LONG *,
  234. *                       LONG *,BOOL);
  235. *
  236. *   FUNCTION
  237. *    In order to make room for the level text displayed by a
  238. *    SLIDER_KIND object one needs to know how much space the
  239. *    longest level string will occupy. Otherwise, the level
  240. *    text may overwrite the gadget label text or the slider
  241. *    container. This routine will rattle through all possible
  242. *    slider settings (as given via the Min and the Max
  243. *    level values) and determine the longest label string
  244. *    according to the font used.
  245. *
  246. *   INPUTS
  247. *    Handle - Pointer to a LayoutHandle structure, as returned by
  248. *        a call to LT_CreateHandleTags().
  249. *
  250. *    FormatString - The sprintf() style formatting string to be used
  251. *        to format the slider level settings into text.
  252. *        This is the same string you would pass in via
  253. *        the GTSL_LevelFormat tag when creating the
  254. *        slider object.
  255. *        Default: "%lD" for systems which have locale.library
  256. *            installed, "%ld" otherwise.
  257. *
  258. *    DispFunc - A pointer to the function to filter the slider level
  259. *        values. The result of this function will then be
  260. *        used to format a string into the slider level text.
  261. *        This is the same parameter you would pass in via
  262. *        the GTSL_DispFunc tag when creating the slider
  263. *        object.
  264. *
  265. *        NOTE: the routine will be called with a NULL Gadget
  266. *            parameter, make sure your code will handle
  267. *            this nicely.
  268. *
  269. *        Default: no display function
  270. *
  271. *    Min - The smallest value the slider can be set to. This is
  272. *        same value you would pass in via GTSL_Min when creating
  273. *        the slider object.
  274. *
  275. *    Max - The largest value the slider can be set to. This is
  276. *        same value you would pass in via GTSL_Max when creating
  277. *        the slider object.
  278. *
  279. *    MaxWidth - Pointer to a place to store the width of the
  280. *        longest level string in pixels. If you pass
  281. *        in NULL instead of the address of a variable
  282. *        no harm will be done.
  283. *
  284. *    MaxLen - Pointer to a place to store the length of the
  285. *        longest level string in characters. If you pass
  286. *        in NULL instead of the address of a variable
  287. *        no harm will be done.
  288. *
  289. *    FullCheck - TRUE will cause the code to rattle through all
  290. *        possible slider settings, starting from the
  291. *        minimum value, ending at the maximum value.
  292. *        While this may be a good idea for a display
  293. *        function to map slider levels to text strings
  294. *        of varying length it might be a problem when
  295. *        it comes to display a range of numbers from
  296. *        1 to 40,000: the code will loop through
  297. *        40,000 iterations trying to find the longest
  298. *        string.
  299. *
  300. *        FALSE will cause the code to calculate the
  301. *        longest level string based only on the
  302. *        minimum and the maximum value to check.
  303. *        While this is certainly a good a idea when
  304. *        it comes to display a range of numbers from
  305. *        1 to 40,000 as only two values will be
  306. *        checked the code may fail to produce
  307. *        accurate results for sliders using display
  308. *        functions mapping slider levels to strings.
  309. *
  310. *   RESULT
  311. *    Index - The slider level which gives the longest
  312. *        level string.
  313. *
  314. *   NOTES
  315. *    This function does exactly what gtlayout.library/LT_LevelWidth
  316. *    does, but uses a slightly different register ordering. Namely,
  317. *    the MaxLen pointer is passed in D3 instead of A5.
  318. *
  319. *   SEE ALSO
  320. *    gtlayout.library/LT_LevelWidth
  321. *
  322. ******************************************************************************
  323. *
  324. */
  325.  
  326. VOID LIBENT
  327. LT_NewLevelWidth(REG(a0) LayoutHandle *handle,REG(a1) STRPTR levelFormat,REG(a2) DISPFUNC dispFunc,REG(d0) LONG min,REG(d1) LONG max,REG(a3) LONG *maxWidth,REG(d3) LONG *maxLen,REG(d2) BOOL fullCheck)
  328. {
  329.     LTP_LevelWidth(handle,levelFormat,dispFunc,min,max,maxWidth,maxLen,fullCheck);
  330. }
  331.